home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / gs / gdevtiff.c next >
C/C++ Source or Header  |  1994-08-01  |  27KB  |  774 lines

  1. /* $Header: /usr/people/sam/fax/gs/RCS/gdevtiff.c,v 1.8 1993/09/05 13:41:14 sam Rel $ */
  2.  
  3. /*
  4.  * Copyright (c) 1992, 1993 Sam Leffler
  5.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26. /* gdevtiff.c */
  27.  
  28. #include "gdevprn.h"
  29. #include "gdevdfg3.h"
  30. #include "gdevtiff.h"
  31.  
  32. #ifdef __PROTOTYPES__
  33. #    define PROTO_ARGS(X)    X
  34. #else
  35. #    define PROTO_ARGS(X)    ()
  36. #endif
  37.  
  38. /*
  39.  * TIFF fax output driver.
  40.  */
  41. typedef struct {
  42.     FILE*    fp;
  43.     long    prevdir;    /* file offset of previous directory offset */
  44.     long    diroff;        /* file offset of next write */
  45.     int        bigendian;    /* 1 if machine is big-endian, 0 otherwise */
  46.     unsigned long iwidth;    /* width of image data in pixels */
  47.     int        fax_byte;
  48.     int        fax_weight;
  49. } TIFFOUT;
  50.  
  51. private void    faxout_open_fp PROTO_ARGS((FILE *, TIFFOUT*));
  52. private int    faxout_begin_page PROTO_ARGS((TIFFOUT*, gx_device_printer*));
  53. private int    faxout_eolcode PROTO_ARGS((TIFFOUT *));
  54. private int    faxout_end_page PROTO_ARGS((TIFFOUT *));
  55. private void    tofax PROTO_ARGS((TIFFOUT*, unsigned char*));
  56.  
  57. private void putwhitespan();
  58. private void putblackspan();
  59. private void putcode();
  60. private void puteol();
  61. private void putbit();
  62. private void flushbits();
  63.  
  64. /*
  65.  * Redefine the device descriptor.
  66.  */
  67. struct gx_device_tiff_s {
  68.     gx_device_common;
  69.     gx_prn_device_common;
  70.     TIFFOUT    fax;
  71. };
  72. typedef struct gx_device_tiff_s gx_device_tiff;
  73.  
  74. /* The device descriptor */
  75. #define X_DPI 204
  76. #define Y_DPI 196
  77. #define LINE_SIZE ((X_DPI * 101 / 10 + 7) / 8)    /* bytes per line */
  78.  
  79. private dev_proc_open_device(tiff_prn_open);
  80. private dev_proc_print_page(tiff_print_page);
  81. private dev_proc_close_device(tiff_prn_close);
  82.  
  83. gx_device_procs tiff_std_procs =
  84.     prn_procs(tiff_prn_open, gdev_prn_output_page, tiff_prn_close);
  85.  
  86. gx_device_tiff far_data gs_tiffg3_device =
  87. {   prn_device_std_body(
  88.     gx_device_tiff,
  89.     tiff_std_procs,
  90.     "tiffg3",
  91.     85,            /* width_10ths, 8.5" */
  92.     110,            /* height_10ths, 11" */
  93.     X_DPI, Y_DPI,
  94.     0,0,0,0,        /* margins */
  95.     1,
  96.     tiff_print_page
  97.     )
  98. };
  99.  
  100.  
  101. static struct pageinfo {
  102.     short w, h;            /* page width and height in 10ths */
  103.     unsigned long iw;        /* image width */
  104. } pageinfo[] = {
  105. #define    PAPER_SIZE_LETTER    0
  106.     { 85,  110, 1728 },
  107. #define    PAPER_SIZE_A4        1
  108.     { 85,  116, 1728 },
  109. #define    PAPER_SIZE_B4        2
  110.     { 101,  143, 2048 }
  111. };
  112. #define    NPAGEINFO (sizeof (pageinfo) / sizeof (pageinfo[0]))
  113.  
  114. /* Get the paper size code, based on width and height. */
  115. static int
  116. papersize(gx_device *dev)
  117. {
  118.     return
  119.       (dev->height / dev->y_pixels_per_inch >= 11.8 ? PAPER_SIZE_B4 :
  120.        dev->height / dev->y_pixels_per_inch >= 11.1 ? PAPER_SIZE_A4 :
  121.        PAPER_SIZE_LETTER);
  122. }
  123.  
  124. /*
  125.  * Driver entry points.
  126.  */
  127.  
  128. /*
  129.  * Setup device according to output page.
  130.  */
  131. private int
  132. tiff_prn_open(gx_device *pdev)
  133. {
  134.     struct pageinfo* pi = &pageinfo[papersize(pdev)];
  135.     int    rc;
  136.  
  137.     pdev->width = (int)((pi->w * pdev->x_pixels_per_inch) / 10);
  138.     pdev->height = (int)((pi->h * pdev->y_pixels_per_inch) / 10);
  139.     rc = gdev_prn_open(pdev);
  140.     if (rc == 0) {
  141.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  142.     faxout_open_fp(ddev->file, &ddev->fax);
  143.     ddev->fax.iwidth = pi->iw;
  144.     }
  145.     return (rc);
  146. }
  147.  
  148. private int
  149. tiff_print_page(gx_device_printer *pdev, FILE *prn_stream)
  150. {
  151.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  152.     unsigned char data[LINE_SIZE + 4];
  153.     int    lnum, line_size;
  154.     TIFFOUT* fax = &ddev->fax;
  155.  
  156.     /* For some odd reason, the file isn't open until now */
  157.     fax->fp = prn_stream;    
  158.     faxout_begin_page(fax, pdev);
  159.     line_size = gdev_mem_bytes_per_scan_line((gx_device*)pdev);
  160.     for (lnum = 0; lnum < pdev->height; lnum++) {
  161.     gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  162.     tofax(fax, data);
  163.     }
  164.     faxout_end_page(fax);
  165.     return (0);
  166. }
  167.  
  168. private int
  169. tiff_prn_close(gx_device *pdev)
  170. {
  171.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  172.     TIFFOUT* fax = &ddev->fax;
  173.  
  174.     if (fax->fp)
  175.     fflush(fax->fp);
  176.     return (gdev_prn_close(pdev));
  177. }
  178.  
  179. /*
  180.  * Internal routines.
  181.  */
  182. private void
  183. faxout_open_fp(FILE *fp, register TIFFOUT* faxp)
  184. {
  185.     faxp->fp = fp;
  186.     faxp->diroff = 0L;
  187.     faxp->prevdir = 0L;
  188.     faxp->bigendian = arch_is_big_endian;
  189.     faxp->fax_byte = 0;
  190.     faxp->fax_weight = 0x01;
  191. }
  192.  
  193. /* NB: this array is sorted by tag number (assumed below) */
  194. typedef struct {
  195.     TIFFDirEntry    subfiletype;
  196.     TIFFDirEntry    imagewidth;
  197.     TIFFDirEntry    imagelength;
  198.     TIFFDirEntry    bitspersample;
  199.     TIFFDirEntry    compression;
  200.     TIFFDirEntry    photometric;
  201.     TIFFDirEntry    fillorder;
  202. #ifdef notdef
  203.     TIFFDirEntry    documentname;
  204. #endif
  205.     TIFFDirEntry    stripoffsets;
  206.     TIFFDirEntry    orientation;
  207.     TIFFDirEntry    samplesperpixel;
  208.     TIFFDirEntry    rowsperstrip;
  209.     TIFFDirEntry    stripbytecounts;
  210.     TIFFDirEntry    xresolution;
  211.     TIFFDirEntry    yresolution;
  212.     TIFFDirEntry    planarconfig;
  213.     TIFFDirEntry    group3options;
  214.     TIFFDirEntry    resolutionunit;
  215. #ifdef notdef
  216.     TIFFDirEntry    software;
  217. #endif
  218.     TIFFDirEntry    cleanfaxdata;
  219.     unsigned long    diroff;            /* offset to next directory */
  220.     unsigned long    xresValue[2];        /* xresolution indirect value */
  221.     unsigned long    yresValue[2];        /* yresolution indirect value */
  222. } TIFFDirectory;
  223. private TIFFDirectory dirTemplate = {
  224.     { TIFFTAG_SUBFILETYPE,    TIFF_LONG,  1, FILETYPE_PAGE },
  225.     { TIFFTAG_IMAGEWIDTH,    TIFF_LONG,  1 },
  226.     { TIFFTAG_IMAGELENGTH,    TIFF_LONG,  1 },
  227.     { TIFFTAG_BITSPERSAMPLE,    TIFF_SHORT, 1, 1 },
  228.     { TIFFTAG_COMPRESSION,    TIFF_SHORT, 1, COMPRESSION_CCITTFAX3 },
  229.     { TIFFTAG_PHOTOMETRIC,    TIFF_SHORT, 1, PHOTOMETRIC_MINISWHITE },
  230.     { TIFFTAG_FILLORDER,    TIFF_SHORT, 1, FILLORDER_LSB2MSB },
  231. #ifdef notdef
  232.     { TIFFTAG_DOCUMENTNAME,    TIFF_ASCII, 1 },
  233. #endif
  234.     { TIFFTAG_STRIPOFFSETS,    TIFF_LONG,  1 },
  235.     { TIFFTAG_ORIENTATION,    TIFF_SHORT, 1, ORIENTATION_TOPLEFT },
  236.     { TIFFTAG_SAMPLESPERPIXEL,    TIFF_SHORT, 1, 1 },
  237.     { TIFFTAG_ROWSPERSTRIP,    TIFF_LONG,  1, -1L },
  238.     { TIFFTAG_STRIPBYTECOUNTS,    TIFF_LONG,  1, 1 },
  239.     { TIFFTAG_XRESOLUTION,    TIFF_RATIONAL, 1 },
  240.     { TIFFTAG_YRESOLUTION,    TIFF_RATIONAL, 1 },
  241.     { TIFFTAG_PLANARCONFIG,    TIFF_SHORT, 1, PLANARCONFIG_CONTIG },
  242.     { TIFFTAG_GROUP3OPTIONS,    TIFF_LONG,  1 },
  243.     { TIFFTAG_RESOLUTIONUNIT,    TIFF_SHORT, 1, RESUNIT_INCH },
  244. #ifdef notdef
  245.     { TIFFTAG_SOFTWARE,        TIFF_ASCII, 1 },
  246. #endif
  247.     { TIFFTAG_CLEANFAXDATA,    TIFF_SHORT, 1, CLEANFAXDATA_CLEAN },
  248.     0, { 0, 1 }, { 0, 1 },
  249. };
  250. #define    OFFSET(x)    ((unsigned)&(((TIFFDirectory*)0)->x))
  251. #define    NTAGS        (OFFSET(diroff) / sizeof (TIFFDirEntry))
  252.  
  253. /* correct tag values on bigendian machines */
  254. private void
  255. faxout_fixuptags(TIFFDirEntry* dp, int n)
  256. {
  257.     while (n-- > 0) {
  258.     if (dp->tdir_type == TIFF_SHORT || dp->tdir_type == TIFF_SSHORT)
  259.         dp->tdir_offset <<= 16;
  260.     else if (dp->tdir_type == TIFF_BYTE || dp->tdir_type == TIFF_SBYTE)
  261.         dp->tdir_offset <<= 24;
  262.     dp++;
  263.     }
  264. }
  265.  
  266. private int
  267. faxout_begin_page(TIFFOUT *faxp, gx_device_printer* pdev)
  268. {
  269.     gx_device_tiff* ddev = (gx_device_tiff*) pdev;
  270.     short dircount;
  271.     TIFFDirectory dir;
  272.  
  273.     /*
  274.      * If Ghostscript was invoked with -sOutputFile=file.%d, then
  275.      * it is expected to generate a new file for each page.  This
  276.      * makes no sense for TIFF, but in the interest of compatibility
  277.      * we do the right thing here.
  278.      */
  279.     if (gdev_prn_file_is_new(pdev))
  280.         faxp->diroff = 0;
  281.     /*
  282.      * Writing the header is delayed to here because the
  283.      * FILE* is not setup when faxout_open is called.
  284.      */
  285.     if (faxp->diroff == 0) {
  286.     TIFFHeader h;
  287.     h.tiff_magic = (faxp->bigendian ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN);
  288.     h.tiff_version = TIFF_VERSION;
  289.     h.tiff_diroff = sizeof (TIFFHeader);
  290.     fwrite((char*) &h, sizeof (h), 1, faxp->fp);
  291.     faxp->diroff = sizeof (TIFFHeader);    /* where next directory goes */
  292.     } else {
  293.     /* patch pointer to this directory */
  294.     fseek(faxp->fp, faxp->prevdir, 0);
  295.     fwrite((char*)&faxp->diroff, sizeof (faxp->diroff), 1, faxp->fp);
  296.     }
  297.     fseek(faxp->fp, faxp->diroff, 0);
  298.     /* write count of tags in directory */
  299.     dircount = NTAGS;
  300.     fwrite((char*)&dircount, sizeof (dircount), 1, faxp->fp);
  301.     faxp->diroff += sizeof (dircount);
  302.  
  303.     /* fill in directory tags and write them */
  304.     memcpy(&dir, &dirTemplate, sizeof (dirTemplate));
  305.     dir.imagewidth.tdir_offset = pageinfo[papersize((gx_device*) pdev)].iw;
  306.     dir.imagelength.tdir_offset = pdev->height;
  307.     dir.stripoffsets.tdir_offset = faxp->diroff + sizeof (TIFFDirectory);
  308.     dir.xresolution.tdir_offset = faxp->diroff + OFFSET(xresValue);
  309.     dir.yresolution.tdir_offset = faxp->diroff + OFFSET(yresValue);
  310.     dir.group3options.tdir_offset = 0;        /* XXX */
  311.     dir.xresValue[0] = ddev->x_pixels_per_inch;
  312.     dir.yresValue[0] = ddev->y_pixels_per_inch;
  313.     if (faxp->bigendian)
  314.     faxout_fixuptags(&dir.subfiletype, NTAGS);
  315.     fwrite((char*)&dir, sizeof (dir), 1, faxp->fp);
  316.  
  317.     puteol(faxp);
  318.     return (0);
  319. }
  320.  
  321. private int
  322. faxout_end_page(TIFFOUT *faxp)
  323. {
  324.     long diroff, cc;
  325.  
  326.     flushbits(faxp);
  327.     diroff = faxp->diroff;
  328.     faxp->prevdir = faxp->diroff + OFFSET(diroff);
  329.     faxp->diroff = ftell(faxp->fp);
  330.     /* patch strip byte counts value */
  331.     cc = faxp->diroff - (diroff + sizeof (TIFFDirectory));
  332.     fseek(faxp->fp, diroff + OFFSET(stripbytecounts.tdir_offset), 0);
  333.     fwrite(&cc, sizeof (cc), 1, faxp->fp);
  334.     return (0);
  335. }
  336.  
  337. private const byte far_data b_run_tbl[8][256] = {
  338.   {    /* START BIT 0 */
  339.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  340.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  341.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  342.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  343.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  344.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  345.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  346.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  347.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  348.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  349.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  350.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  351.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  352.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  353.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  354.     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
  355.   },
  356.   {    /* START BIT 1 */
  357.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  358.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  359.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  360.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  361.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  362.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  363.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  364.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  365.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  366.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  367.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  368.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  369.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  370.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  371.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  372.     0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 
  373.   },
  374.   {    /* START BIT 2 */
  375.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  376.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  377.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  378.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  379.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  380.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  381.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  382.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  383.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  384.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  385.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  386.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  387.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  388.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  389.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  390.     0, 0, 0, 0, 1, 1, 2, 3, 0, 0, 0, 0, 1, 1, 2, 3, 
  391.   },
  392.   {    /* START BIT 3 */
  393.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  394.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  395.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  396.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  397.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  398.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  399.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  400.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  401.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  402.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  403.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  404.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  405.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  406.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  407.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  408.     0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4, 
  409.   },
  410.   {    /* START BIT 4 */
  411.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  412.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  413.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  414.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  415.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  416.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  417.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  418.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  419.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  420.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  421.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  422.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  423.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  424.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  425.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  426.     1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 
  427.   },
  428.   {    /* START BIT 5 */
  429.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  430.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  431.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  432.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  433.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  434.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  435.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  436.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  437.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  438.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  439.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  440.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  441.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  442.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  443.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  444.     2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 
  445.   },
  446.   {    /* START BIT 6 */
  447.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  448.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  449.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  450.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  451.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  452.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  453.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  454.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  455.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  456.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  457.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  458.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  459.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  460.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  461.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  462.     3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7, 
  463.   },
  464.   {    /* START BIT 7 */
  465.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  466.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  467.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  468.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  469.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  470.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  471.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  472.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  473.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  474.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  475.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  476.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  477.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  478.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  479.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  480.     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, 
  481.   },
  482. };
  483.  
  484. private const byte far_data w_run_tbl[8][256] = {
  485.   {    /* START BIT 0 */
  486.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  487.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  488.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  489.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  490.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  491.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  492.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  493.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  494.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  495.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  496.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  497.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  498.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  499.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  500.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  501.     1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
  502.   },
  503.   {    /* START BIT 1 */
  504.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  505.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  506.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  507.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  508.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  509.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  510.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  511.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  512.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  513.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  514.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  515.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  516.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  517.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  518.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  519.     2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0, 
  520.   },
  521.   {    /* START BIT 2 */
  522.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  523.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  524.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  525.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  526.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  527.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  528.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  529.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  530.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  531.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  532.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  533.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  534.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  535.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  536.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  537.     3, 2, 1, 1, 0, 0, 0, 0, 3, 2, 1, 1, 0, 0, 0, 0, 
  538.   },
  539.   {    /* START BIT 3 */
  540.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  541.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  542.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  543.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  544.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  545.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  546.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  547.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  548.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  549.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  550.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  551.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  552.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  553.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  554.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  555.     4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  556.   },
  557.   {    /* START BIT 4 */
  558.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  559.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  560.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  561.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  562.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  563.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  564.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  565.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  566.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  567.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  568.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  569.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  570.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  571.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  572.     5, 4, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 
  573.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  574.   },
  575.   {    /* START BIT 5 */
  576.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  577.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  578.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  579.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  580.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  581.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  582.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  583.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  584.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  585.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  586.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  587.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  588.     6, 5, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 
  589.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  590.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  591.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  592.   },
  593.   {    /* START BIT 6 */
  594.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  595.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  596.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  597.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  598.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  599.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  600.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  601.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  602.     7, 6, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 
  603.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  604.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  605.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  606.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  607.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  608.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  609.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  610.   },
  611.   {    /* START BIT 7 */
  612.     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 
  613.     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  614.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  615.     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
  616.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  617.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  618.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  619.     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  620.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  621.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  622.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  623.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  624.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  625.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  626.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  627.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  628.   },
  629. };
  630.  
  631. /*
  632.  * Macros to use tables in findrun.c
  633.  * Input is *p, bit
  634.  * Output is rl, *p, bit
  635.  */
  636. #define    find_black_run()                    \
  637.     for (rl = 0;;) {                        \
  638.     if (run = b_run_tbl[bit][*p]) {                \
  639.         rl += run;                        \
  640.         bit -= run;                        \
  641.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  642.     }                            \
  643.     break;                            \
  644.     }
  645. #define    find_white_run()                    \
  646.     for (rl = 0;;) {                        \
  647.     if (run = w_run_tbl[bit][*p]) {                \
  648.         rl += run;                        \
  649.         bit -= run;                        \
  650.         if (bit < 0 && ++p < ep) { bit=7; continue;}    \
  651.     }                            \
  652.     break;                            \
  653.     }
  654.  
  655. private void
  656. tofax(TIFFOUT *faxp, unsigned char *p)
  657. {
  658.     unsigned char    *ep;
  659.     register int    bit;
  660.     register int    run;
  661.     register int    rl;
  662.  
  663.     ep = p + faxp->iwidth/8;
  664.     bit = 7;
  665.     for (;;) {
  666.     find_white_run();
  667.     putwhitespan(faxp, rl);
  668.     if (p >= ep) break;
  669.  
  670.     find_black_run();
  671.     putblackspan(faxp, rl);
  672.     if (p >= ep) break;
  673.     }
  674.     puteol(faxp);
  675. }
  676.  
  677.  
  678. /*************************************************************************
  679. **
  680. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  681. **
  682. ** Permission to use, copy, modify, and distribute this software and its
  683. ** documentation for any purpose and without fee is hereby granted, provided
  684. ** that the above copyright notice appear in all copies and that both that
  685. ** copyright notice and this permission notice appear in supporting
  686. ** documentation.  This software is provided "as is" without express or
  687. ** implied warranty.
  688.  *************************************************************************/
  689.  
  690. private void
  691. putwhitespan(register TIFFOUT *faxp, int c)
  692. {
  693.     register int tpos;
  694.     register tableentry* te;
  695.  
  696.     if(c>=64) {
  697.     tpos = (c/64)-1;
  698.     te = tpos < 27 ? mwtable+tpos : extable+tpos-27;
  699.     c -= te->count;
  700.     putcode(faxp, te);
  701.     }
  702.     tpos = c;
  703.     te = twtable+tpos;
  704.     putcode(faxp, te);
  705. }
  706.  
  707. private void
  708. putblackspan(register TIFFOUT *faxp, int c)
  709. {
  710.     register int tpos;
  711.     register tableentry* te;
  712.  
  713.     if(c>=64) {
  714.     tpos = (c/64)-1;
  715.     te = tpos < 27 ? mbtable+tpos : extable+tpos-27;
  716.     c -= te->count;
  717.     putcode(faxp, te);
  718.     }
  719.     tpos = c;
  720.     te = tbtable+tpos;
  721.     putcode(faxp, te);
  722. }
  723.  
  724. private void
  725. putcode(register TIFFOUT *faxp, tableentry *te)
  726. {
  727.     register unsigned int mask;
  728.     register int code;
  729.  
  730.     mask = 1<<(te->length-1);
  731.     code = te->code;
  732.     while(mask) {
  733.      if(code&mask)
  734.         putbit(faxp, 1);
  735.     else
  736.         putbit(faxp, 0);
  737.     mask >>= 1;
  738.     }
  739.  
  740. }
  741.  
  742. private void
  743. puteol(register TIFFOUT *faxp)
  744. {
  745.     register int i;
  746.  
  747.     for(i=0; i<11; ++i)
  748.     putbit(faxp, 0);
  749.     putbit(faxp, 1);
  750. }
  751.  
  752. private void
  753. putbit(register TIFFOUT *faxp, int d)
  754. {
  755.     if(d) 
  756.     faxp->fax_byte = faxp->fax_byte|faxp->fax_weight;
  757.     faxp->fax_weight = faxp->fax_weight<<1;
  758.     if((faxp->fax_weight&0xff) == 0) {
  759.     putc(faxp->fax_byte, faxp->fp);
  760.     faxp->fax_byte = 0;
  761.     faxp->fax_weight = 0x01;
  762.     }
  763. }
  764.  
  765. private void
  766. flushbits(register TIFFOUT *faxp)
  767. {
  768.     if (faxp->fax_weight != 0x01) {
  769.     putc(faxp->fax_byte, faxp->fp);
  770.     faxp->fax_byte = 0;
  771.     faxp->fax_weight = 0x01;
  772.     }
  773. }
  774.